﻿//-----------------------------------------------------------------------
// <copyright file="PlanSelector.cs" company="Redmist">
//     Copyright (c) Damien T Smith. All rights reserved.
// </copyright>
// <author>Damien Smith</author>
//-----------------------------------------------------------------------

namespace Redmist.UsageMonitor.CustomControls
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    /// <summary>A custom GroupBox control which allows selection of aaNet plan parameters.</summary>
    public partial class PlanSelector : UserControl
    {
        /// <summary>A Redmist.UsageMonitor.CustomControls.PlanType representing an aaNet plan type.</summary>
        private PlanType planType;

        /// <summary>A System.Collections.Generic.List of Redmist.UsageMonitor.CustomControls.OffPeakRatio representing the off peak plan choices datasource.</summary>
        private List<OffPeakRatio> ratios;

        /// <summary>Initializes a new instance of the PlanSelector class.</summary>
        public PlanSelector()
        {
            this.InitializeComponent();
            this.CurrentPlanType = PlanType.ReducedOffPeak;
            this.InitializeOffPeakSelector();
        }

        /// <summary>Occurs when any plan type parameters are changed.</summary>
        public event EventHandler ParametersChanged;

        /// <summary>Gets or sets a Redmist.UsageMonitor.CustomControls.PlanType representing the currently selected aaNet plan type.</summary>
        public PlanType CurrentPlanType
        {
            get
            {
                return this.planType;
            }

            set
            {
                this.planType = value;
                switch (this.planType)
                {
                    case PlanType.ReducedOffPeak:
                        this.reducedOffPeakRadioButton.Checked = true;
                        break;
                    case PlanType.FixedOffPeak:
                        this.fixedOffPeakRadioButton.Checked = true;
                        break;
                    case PlanType.UnlimitedOffPeak:
                        this.unlimitedOffPeakRadioButton.Checked = true;
                        break;
                    case PlanType.PayAsYouGo:
                        this.payAsYouGoRadioButton.Checked = true;
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>Gets or sets a System.String representing a reduced off-peak plan.</summary>
        public int SelectedOffPeakRatio
        {
            get
            {
                return (this.offPeakPlanComboBox.SelectedItem as OffPeakRatio).Ratio;
            }

            set
            {
                foreach (OffPeakRatio item in this.ratios)
                {
                    if (item.Ratio == value)
                    {
                        this.offPeakPlanComboBox.SelectedIndex = this.ratios.IndexOf(item);
                    }
                }
            }
        }

        /// <summary>Gets or sets the gigabyte quota for a fixed off-peak plan.</summary>
        public int FixedQuotaGigabytes
        {
            get
            {
                return (int)this.fixedOffPeakQuotaNumericUpDown.Value;
            }

            set
            {
                this.fixedOffPeakQuotaNumericUpDown.Value = value;
            }
        }

        /// <summary>Raises the PlanSelector.ParametersChanged event.</summary>
        /// <param name="e">A System.EventArgs that contains the event data.</param>
        protected virtual void OnParametersChanged(EventArgs e)
        {
            if (this.ParametersChanged != null)
            {
                this.ParametersChanged(this, e);
            }
        }

        /// <summary>Sets up the off peak selection combo box options.</summary>
        private void InitializeOffPeakSelector()
        {
            this.ratios = new List<OffPeakRatio>();
            this.ratios.Add(new OffPeakRatio() { PercentText = "50%", Ratio = 2 });
            this.ratios.Add(new OffPeakRatio() { PercentText = "33%", Ratio = 3 });
            this.ratios.Add(new OffPeakRatio() { PercentText = "10%", Ratio = 10 });
            this.offPeakRatioBindingSource.DataSource = this.ratios;
            this.offPeakPlanComboBox.SelectedIndex = 0;
        }

        /// <summary>Handles the CheckedChanged event for the 'Reduced Off Peak' radio button.</summary>
        /// <param name="sender">A System.Object representing the sending object.</param>
        /// <param name="e">A System.EventArgs representing the event arguments.</param>
        private void ReducedOffPeakRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (this.reducedOffPeakRadioButton.Checked)
            {
                this.planType = PlanType.ReducedOffPeak;
                this.offPeakPlanComboBox.Enabled = true;
                this.OnParametersChanged(e);
            }
            else
            {
                this.offPeakPlanComboBox.Enabled = false;
            }
        }

        /// <summary>Handles the CheckedChanged event for the 'Fixed Off Peak' radio button.</summary>
        /// <param name="sender">A System.Object representing the sending object.</param>
        /// <param name="e">A System.EventArgs representing the event arguments.</param>
        private void FixedOffPeakRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (this.fixedOffPeakRadioButton.Checked)
            {
                this.planType = PlanType.FixedOffPeak;
                this.fixedOffPeakQuotaNumericUpDown.Enabled = true;
                this.OnParametersChanged(e);
            }
            else
            {
                this.fixedOffPeakQuotaNumericUpDown.Enabled = false;
            }
        }

        /// <summary>Handles the CheckedChanged event for the 'Unlimited Off Peak' radio button.</summary>
        /// <param name="sender">A System.Object representing the sending object.</param>
        /// <param name="e">A System.EventArgs representing the event arguments.</param>
        private void UnlimitedOffPeakRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (this.unlimitedOffPeakRadioButton.Checked)
            {
                this.planType = PlanType.UnlimitedOffPeak;
                this.OnParametersChanged(e);
            }
        }

        /// <summary>Handles the CheckedChanged event for the 'Pay As You Go' radio button.</summary>
        /// <param name="sender">A System.Object representing the sending object.</param>
        /// <param name="e">A System.EventArgs representing the event arguments.</param>
        private void PayAsYouGoRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (this.payAsYouGoRadioButton.Checked)
            {
                this.planType = PlanType.PayAsYouGo;
                this.OnParametersChanged(e);
            }
        }

        /// <summary>Handles the SelectedIndexChanged event for the 'Off Peak Plan' combo box.</summary>
        /// <param name="sender">A System.Object representing the sending object.</param>
        /// <param name="e">A System.EventArgs representing the event arguments.</param>
        private void OffPeakPlanComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OnParametersChanged(e);
        }

        /// <summary>Handles the ValueChanged event for the 'Fixed Off Peak Quota' numeric up down.</summary>
        /// <param name="sender">A System.Object representing the sending object.</param>
        /// <param name="e">A System.EventArgs representing the event arguments.</param>
        private void FixedOffPeakQuotaNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.OnParametersChanged(e);
        }
    }
}
